﻿using Microsoft.ServiceBus;
using Microsoft.ServiceBus.Messaging;
using Microsoft.WindowsAzure.Storage;
using Microsoft.WindowsAzure.Storage.Queue;
using Microsoft.WindowsAzure.Storage.Table;
using System;

namespace setup
{
    class setup
    {
        // Name of queues
        private const string HIGHQ = "highq";
        private const string MEDIUMQ = "mediumq";
        private const string LOWQ = "lowq";
        private const string IDLE = "idlequeue";
        //name of job table
        private const string TABLE = "jobtable";
        //name of command queue
        private const string CQNAME = "cqueue";
        //name of status queue
        private const string RQNAME = "outqueue";
        //name of service bus
        private const string SBNAME = "<service namespace>";
        //service bus key
        private const string SBKEY = "<service bus key>";
        //service bus owner
        private const string SBISSUER = "owner";
        private const string _sbconnectionstring = "Endpoint=sb://<service namespace>.servicebus.windows.net;SharedSecretIssuer=owner;SharedSecretValue=<service bus key>";


        // set these constants with your values to run the sample.

        private const string _storageacc = "<storage account name>";
        private const string _storagekey = "<storage key>";
        private const string _connectionstring = "DefaultEndpointsProtocol=https;AccountName=<storage account name>;AccountKey=53A4/9CSO0HM53bwHe8Jpw8Nlo736onZtPPnhVHKLzbjupOMGbi12eFq1USzPUGEpK3jK+/UxdDoiZeRBExe8Q==";
        // Time to live for messages on sb topics
        static TimeSpan _maxttl = TimeSpan.FromDays(7);


        static void Main(string[] args)
        {

            CloudStorageAccount storageAccount = CloudStorageAccount.Parse(_connectionstring);
            CloudQueueClient queueclient = storageAccount.CreateCloudQueueClient();
            CloudQueue highq = queueclient.GetQueueReference(HIGHQ);
            highq.CreateIfNotExists();
            CloudQueue mediumq = queueclient.GetQueueReference(MEDIUMQ);
            mediumq.CreateIfNotExists();
            CloudQueue lowq = queueclient.GetQueueReference(LOWQ);
            lowq.CreateIfNotExists();
            Console.WriteLine("High, medium and low priority queues created: {0}, {1}, {2}", HIGHQ, MEDIUMQ, LOWQ);
            CloudQueue idlequeue = queueclient.GetQueueReference(IDLE);
            idlequeue.CreateIfNotExists();
            Console.WriteLine("Idle nodes queue: {0} created.", IDLE);

            CloudTableClient tableclient = storageAccount.CreateCloudTableClient();
            CloudTable jobtable = tableclient.GetTableReference(TABLE);
            jobtable.CreateIfNotExists();
            Console.WriteLine("Job tracking table: {0} created.", TABLE);

            var sbnamespace = NamespaceManager.CreateFromConnectionString(_sbconnectionstring);
            //create command and result topics if they do not exist
            TopicDescription ctd = new TopicDescription(CQNAME);
            ctd.DefaultMessageTimeToLive = _maxttl;
            TopicDescription rtd = new TopicDescription(RQNAME);
            rtd.DefaultMessageTimeToLive = _maxttl;
            if (!sbnamespace.TopicExists(CQNAME)) sbnamespace.CreateTopic(CQNAME);
            Console.WriteLine("Command topic: {0} created.", CQNAME);
            if (!sbnamespace.TopicExists(RQNAME)) sbnamespace.CreateTopic(RQNAME);
            Console.WriteLine("Results topic: {0} created.", RQNAME);
        }
    }
}
